/**
 * Program   :   ListYAMLConfigBuilder.java
 * Author    :   shadow guo
 * Create    :   2013-6-3 下午6:32:15
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.web.core.service.impl.list;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.yaml.snakeyaml.Yaml;

import com.tongfusoft.utils.Logger;
import com.tongfusoft.web.core.action.ExportDefine;

public class ListYAMLParser {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(ListYAMLParser.class);

	public Map<String, ListResourceElement> parser(String namespace,
			Map<String, ListResourceElement> map) {
		String configFile = namespace + "list.yaml";
		Logger.info(String.format("loading config: %s", configFile));

		Yaml yaml = new Yaml();

		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		try {
			for (Object config : yaml.loadAll(loader
					.getResourceAsStream(configFile))) {

				Map<String, Object> properties = (Map<String, Object>) config;
				String key = properties.keySet().iterator().next();

				if (properties.get(key) instanceof Map) {
					// logger.info("===============" + key + "=============");
					Map<String, Object> params = (Map<String, Object>) properties
							.get(key);
					/*
					 * Iterator<Entry<String,Object>> iter =
					 * params.entrySet().iterator(); while(iter.hasNext()){
					 * Entry<String,Object> entry = iter.next();
					 * logger.info("***" + entry.getKey() + ":" +
					 * entry.getValue()); }
					 */
					ListResourceElement element = new ListResourceElement();
					element.setListName(StringUtils.defaultIfEmpty(
							(String) params.get("listName"), ""));
					element.setSelect(StringUtils.defaultIfEmpty(
							(String) params.get("select"), ""));
					element.setFrom(StringUtils.defaultIfEmpty(
							(String) params.get("from"), ""));
					element.setWhere(StringUtils.defaultIfEmpty(
							(String) params.get("where"), ""));
					element.setOrderby(StringUtils.defaultIfEmpty((String)params.get("orderby"), ""));
					
					element.setGroupby(StringUtils.defaultIfEmpty((String)params.get("groupby"), ""));
					
					parserTitleDef(element,params);
					
					map.put("/" + namespace.substring(0,namespace.length()-1) + "#" + key, element);
				}

			}
			Logger.info(String.format("load config: %s end.", configFile));
		} catch (Exception e) {
			logger.error("loading config:" + configFile + " error." + e.getMessage());
		}
		
		return map;
	}
	
	private void parserTitleDef(ListResourceElement element,Map<String, Object> params ){
		if(params.get("titleDefs") != null){
			List<Map> defs = (List<Map>)params.get("titleDefs");
			for(int i=0; i<defs.size() ;i++){
				Map<String,Object> map = defs.get(i);
				ExportDefine expDef = new ExportDefine();
				
				expDef.setKey((String)map.get("key"));
				expDef.setTitle((String)map.get("title"));
				
				if(map.get("isExport") != null){
					expDef.setExport((Boolean)map.get("isExport"));
				}
				
				element.addTitleDef(expDef);
			}
			
			/*Iterator<Entry<String,Object>> iter = defs.entrySet().iterator();
			while(iter.hasNext()){
				Entry<String,Object> entry = iter.next();
				
				ExportDefine expDef = new ExportDefine();
				expDef.setKey(entry.getKey());
				
				Map content = (Map)entry.getValue();
				if(content != null){
					expDef.setTitle((String)content.get("title"));
					
					if(content.get("isExport") != null){
						if(StringUtils.equals((String)content.get("isExport"), "false")){
							expDef.setExport(false);
						}
					}
					
				}
				
				element.addTitleDef(expDef);
			}*/
		}
	}
	

}
